package com.ease.database.initializer;

import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import com.ease.common.util.Detect;
import com.ease.common.util.FileUtil;
import com.ease.common.vo.MapParameter;
import com.ease.dao.JdbcTemplateDataAccessObject;
import com.obas.test.ApplicationContextUserContextTest;

/**
 * @author nathanleewei
 *
 */
public class EquipmentModelInitializer extends ApplicationContextUserContextTest {

	@Autowired
	JdbcTemplateDataAccessObject jdbcTemplateDataAccessObject;

	@Test
	public void test() {
		String string = FileUtil.readClassPathResourceToString(EquipmentModelInitializer.class, "EquipmentModel.txt");

		String[] rows = StringUtils.delimitedListToStringArray(string, "\r\n");
		MapParameter parameter = null;
		if (null != rows) {
			for (String row : rows) {
				String[] values = StringUtils.commaDelimitedListToStringArray(row);
				if (null == values) {
					continue;
				}
				String vendorName = Detect.trim(values[0]);
				String vendorCode = Detect.trim(values[1]);
				String modelCode = Detect.trim(values[2]);
				String entityTypeCode = Detect.trim(values[3]);

				parameter = new MapParameter();
				parameter.put("code", vendorCode);
				//
				List<Map<String, Object>> equipmentVendors = jdbcTemplateDataAccessObject.find("SELECT * FROM M_EQUIPMENTVENDOR T WHERE T.CODE=:code", parameter);
				if (!notEmpty(equipmentVendors)) {
					parameter.put("name", vendorName);
					jdbcTemplateDataAccessObject.create("INSERT INTO M_EQUIPMENTVENDOR(ID, CODE, NAME)VALUES(SEQ_M_EQUIPMENTVENDOR.NEXTVAL, :code, :name)", parameter);

				}
				equipmentVendors = jdbcTemplateDataAccessObject.find("SELECT * FROM M_EQUIPMENTVENDOR T WHERE T.CODE=:code", parameter);
				Map<String, Object> equipmentVendor = firstOne(equipmentVendors);
				long equipmentVendorId = Long.parseLong(String.valueOf(equipmentVendor.get("ID")));

				//
				parameter = new MapParameter();
				parameter.put("code", entityTypeCode);

				List<Map<String, Object>> entityTypes = jdbcTemplateDataAccessObject.find("SELECT * FROM MM_ENTITYTYPE T WHERE T.CODE=:code", parameter);
				if (!notEmpty(entityTypes)) {
					parameter.put("name", entityTypeCode);
					jdbcTemplateDataAccessObject.create("INSERT INTO MM_ENTITYTYPE(ID, CODE, NAME)VALUES(SEQ_MM_ENTITYTYPE.NEXTVAL, :code, :name)", parameter);

				}
				entityTypes = jdbcTemplateDataAccessObject.find("SELECT * FROM MM_ENTITYTYPE T WHERE T.CODE=:code", parameter);
				Map<String, Object> entityType = firstOne(entityTypes);
				long entityTypeId = Long.parseLong(String.valueOf(entityType.get("ID")));

				//

				parameter = new MapParameter();

				parameter.put("code", modelCode);
				List<Map<String, Object>> equipmentModels = jdbcTemplateDataAccessObject.find("SELECT * FROM M_EQUIPMENTMODEL T WHERE T.CODE=:code", parameter);
				if (!notEmpty(equipmentModels)) {
					parameter.put("name", modelCode);
					parameter.put("entityTypeId", entityTypeId);
					parameter.put("entityTypeCode", entityTypeCode);
					parameter.put("equipmentVendorId", equipmentVendorId);

					jdbcTemplateDataAccessObject.create(
							"INSERT INTO M_EQUIPMENTMODEL(ID, CODE, NAME, ENTITYTYPE_ID, ENTITYTYPECODE, EQUIPMENTVENDOR_ID)VALUES(SEQ_M_EQUIPMENTMODEL.NEXTVAL, :code, :name, :entityTypeId,:entityTypeCode, :equipmentVendorId)",
							parameter);
				}


			}
		}

	}

}
